﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data.Entity;


namespace DAL
{
    //评论管理Dal层
    public class DAL_View_CommentAU
    {
        //查询所有的评论以及条件查询评论
        //分页查询                                                 当前页数     每页几条     起始时间         关键字
        public static Pager<View_CommentAU> GetView_CommentAUs(int PageIndex,int PageSize,string cmtime,  string cmhtml)
        {
            using (var db = new KQCMS()) {
               //List<Where> where = new List<Where>();
                //总条数  
                var count = db.View_CommentAU.Count()/* % PageSize == 0 ? db.View_CommentAU.Count() / PageSize : db.View_CommentAU.Count() / PageSize + 1*/;
                //所有的数据
                var list = db.View_CommentAU.OrderBy(x=>x.aid).Skip((PageIndex-1)*PageSize).Take(PageSize).ToList();
                //起始时间
                if (cmtime != null)
                    list = list.Where(x=>x.cmtime >= Convert.ToDateTime(cmtime)).ToList();
                //关键词
                if (!string.IsNullOrEmpty(cmhtml))
                    list = list.Where(x => x.cmhtml.Contains(cmhtml)).ToList();
                Pager<View_CommentAU> p = new Pager<View_CommentAU>();
                p.Count = count;
                p.Data = list;
                return p;
            }
        }
        //添加评论
        public static int AddCMS_Comment(CMS_Comment cm) {
            using (var db = new KQCMS()) {
                db.Entry(cm).State = EntityState.Added;
                if (db.SaveChanges()>0)
                    return 1;
                else
                    return 0;
            }
        }
    }
}
